import { Case, Suspect, Evidence, InvestigationNote } from '../services/MockDataService';

/**
 * AI侦探应用数据处理工具类
 */
export class DataProcessor {
  // 格式化案件状态文本
  static formatCaseStatus(status: string): string {
    const statusMap = {
      pending: '待处理',
      investigating: '调查中',
      solved: '已解决',
      closed: '已结案'
    };
    return statusMap[status as keyof typeof statusMap] || status;
  }

  // 获取案件状态对应的颜色
  static getCaseStatusColor(status: string): string {
    const colorMap = {
      pending: '#86909C',
      investigating: '#1890FF',
      solved: '#00B42A',
      closed: '#722ED1'
    };
    return colorMap[status as keyof typeof statusMap] || '#86909C';
  }

  // 格式化优先级文本
  static formatPriority(priority: string): string {
    const priorityMap = {
      low: '低',
      medium: '中',
      high: '高'
    };
    return priorityMap[priority as keyof typeof priorityMap] || priority;
  }

  // 获取优先级对应的颜色
  static getPriorityColor(priority: string): string {
    const colorMap = {
      low: '#00B42A',
      medium: '#FF7D00',
      high: '#F53F3F'
    };
    return colorMap[priority as keyof typeof priorityMap] || '#86909C';
  }

  // 格式化性别文本
  static formatGender(gender: string): string {
    const genderMap = {
      male: '男',
      female: '女'
    };
    return genderMap[gender as keyof typeof genderMap] || gender;
  }

  // 格式化证据类型文本
  static formatEvidenceType(type: string): string {
    const typeMap = {
      physical: '物证',
      digital: '电子证据',
      testimonial: '人证'
    };
    return typeMap[type as keyof typeof typeMap] || type;
  }

  // 格式化日期显示
  static formatDate(dateString: string): string {
    const date = new Date(dateString);
    return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
  }

  // 按创建时间排序案件
  static sortCasesByCreationDate(cases: Case[], ascending: boolean = true): Case[] {
    return [...cases].sort((a, b) => {
      const dateA = new Date(a.createdAt).getTime();
      const dateB = new Date(b.createdAt).getTime();
      return ascending ? dateA - dateB : dateB - dateA;
    });
  }

  // 按优先级排序案件
  static sortCasesByPriority(cases: Case[], ascending: boolean = false): Case[] {
    const priorityOrder = {
      high: 3,
      medium: 2,
      low: 1
    };
    
    return [...cases].sort((a, b) => {
      const priorityA = priorityOrder[a.priority as keyof typeof priorityOrder] || 0;
      const priorityB = priorityOrder[b.priority as keyof typeof priorityOrder] || 0;
      return ascending ? priorityA - priorityB : priorityB - priorityA;
    });
  }

  // 搜索案件
  static searchCases(cases: Case[], keyword: string): Case[] {
    if (!keyword) return cases;
    
    const lowerKeyword = keyword.toLowerCase();
    return cases.filter(caseItem => 
      caseItem.title.toLowerCase().includes(lowerKeyword) ||
      caseItem.description.toLowerCase().includes(lowerKeyword) ||
      this.formatCaseStatus(caseItem.status).includes(keyword)
    );
  }

  // 统计案件状态分布
  static countCasesByStatus(cases: Case[]): { [key: string]: number } {
    const statusCount: { [key: string]: number } = {};
    
    cases.forEach(caseItem => {
      statusCount[caseItem.status] = (statusCount[caseItem.status] || 0) + 1;
    });
    
    return statusCount;
  }

  // 统计案件优先级分布
  static countCasesByPriority(cases: Case[]): { [key: string]: number } {
    const priorityCount: { [key: string]: number } = {};
    
    cases.forEach(caseItem => {
      priorityCount[caseItem.priority] = (priorityCount[caseItem.priority] || 0) + 1;
    });
    
    return priorityCount;
  }

  // 根据相关性排序证据
  static sortEvidenceByRelevance(evidence: Evidence[], ascending: boolean = false): Evidence[] {
    const relevanceOrder = {
      high: 3,
      medium: 2,
      low: 1
    };
    
    return [...evidence].sort((a, b) => {
      const relevanceA = relevanceOrder[a.relevance as keyof typeof relevanceOrder] || 0;
      const relevanceB = relevanceOrder[b.relevance as keyof typeof relevanceOrder] || 0;
      return ascending ? relevanceA - relevanceB : relevanceB - relevanceA;
    });
  }

  // 获取嫌疑人年龄范围
  static getSuspectAgeRange(suspects: Suspect[]): { min: number; max: number; average: number } {
    if (suspects.length === 0) {
      return { min: 0, max: 0, average: 0 };
    }
    
    const ages = suspects.map(s => s.age);
    const min = Math.min(...ages);
    const max = Math.max(...ages);
    const average = Math.round(ages.reduce((sum, age) => sum + age, 0) / ages.length);
    
    return { min, max, average };
  }
}